<?php
/**
 * (model) CN-海外仓发货单（国内发海外仓）
 */
namespace App\Models\OverSeasIn;

use App\Models\Basics\SysArea;
use App\Models\Basics\SysPlat;
use App\Models\OverSeasBa\Warehouse;
use Illuminate\Database\Eloquent\Model;

class Delivery extends Model
{
    protected $table = 'os_in_delivery';

    protected $guarded = [];

    public static $_status = [
        1 => '待上传标签',
        2 => '待备货',
        3 => '备货中',
        4 => '装箱信息待录入',
        5 => '待发货',
        6 => '已发货',
        7 => '海外仓已入库',
    ];

    public static $_rStatus = [
        6 => '已发货',
        7 => '海外仓已入库',
    ];

    //运单号异常状态
    public static $_trackExStatus = [
        0 => '待录入',
        1 => '未录入',//异常
        2 => '已录入',//正常
    ];

    //运输状态
    public static $_transportStatus = [
        0 => '待开船',
        1 => '已开船',
        2 => '已到港',
        3 => '已提取',
        4 => '已签收',
    ];

    public static $_logisticsStatus = [
        1 => '待更新',
        2 => '已更新',
    ];

    public static $_isFnsku = [
        1 => '是',
        2 => '否',
    ];

    /**
     * 海外仓-发货类型
     */
    const OS_DELIVERY_TRANSFER = 1; //海外仓-中转
    const OS_DELIVERY_DROPSHIPPING = 2; //海外仓-代发 (一件代发)
    public static $_deliveryType = [
        self::OS_DELIVERY_TRANSFER => '中转',
        self::OS_DELIVERY_DROPSHIPPING => '代发'
    ];

    public function detail(){
        return $this->hasMany(DeliveryDetail::class,'delivery_no','delivery_no');
    }

    public function labels(){
        return $this->hasMany(DeliveryLabels::class,'delivery_no','delivery_no');
    }

    public function box(){
        return $this->hasMany(DeliveryBox::class,'delivery_no','delivery_no');
    }

    public function warehouse(){
        return $this->belongsTo(Warehouse::class,'warehouse_id');
    }

    public function area(){
        return $this->belongsTo(SysArea::class,'area_id');
    }

    public function logistics(){
        return $this->hasOne(DeliveryLogistics::class,'delivery_no','delivery_no');
    }

    public function transport(){
        return $this->hasMany(DeliveryTransport::class,'delivery_no','delivery_no');
    }

    public function plat(){
        return $this->belongsTo(SysPlat::class,'plat_id','plat_id');
    }

    protected static function boot()
    {
        parent::boot();

        static::deleting(function($delivery) {
            $delivery->detail()->delete();
        });
    }

    public static $_model = [
        'box' => DeliveryBox::class,
        'detail' => DeliveryDetail::class,
        'delivery' => self::class
    ];

}
